package Test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class keyEventTest {


    private static void key() {
        JFrame frame = new JFrame("key");
        frame.setLayout(new FlowLayout());
        frame.setLocation(300, 200);
        frame.setSize(400, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        JTextField jt = new JTextField( "B",15);
//      JTextField jt2 = new JTextField("A",15);
//      jt2.add(jt);
//      jt.setEditable(false);
        frame.add(jt);
        jt.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                char a = e.getKeyChar();
                int keycode = e.getKeyCode();
                System.out.println("键盘输入" + a);
                System.out.println("字符代码" + keycode);

            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(keyEventTest::key);
    }
}
